package com.campusify.service.impl;

import org.springframework.stereotype.Service;
import com.campusify.compents.PageResult;
import com.campusify.pojo.LostandfoundExample;
import com.campusify.pojo.LostandfoundExample.Criteria;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.campusify.mapper.LostandfoundMapper;
import com.campusify.pojo.Lostandfound;
import com.campusify.service.LostAndFoundService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class LostAndFoundServiceImpl implements LostAndFoundService {

	@Autowired
	private LostandfoundMapper lostAndFoundMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<Lostandfound> findAll() {
		return lostAndFoundMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<Lostandfound> page=   (Page<Lostandfound>) lostAndFoundMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Lostandfound lostAndFound) {
		lostAndFoundMapper.insert(lostAndFound);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(Lostandfound lostAndFound){
		lostAndFoundMapper.updateByPrimaryKey(lostAndFound);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Lostandfound findOne(Integer recordID){
		return lostAndFoundMapper.selectByPrimaryKey(recordID);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] recordIDs) {
		for(Integer recordID:recordIDs){
			lostAndFoundMapper.deleteByPrimaryKey(recordID);
		}		
	}
	
	
		@Override
	public PageResult findPage(Lostandfound lostAndFound, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		LostandfoundExample example=new LostandfoundExample();
		Criteria criteria = example.createCriteria();
		
		if(lostAndFound!=null){			
						if(lostAndFound.getItemname()!=null && lostAndFound.getItemname().length()>0){
				criteria.andItemnameLike("%"+lostAndFound.getItemname()+"%");
			}			if(lostAndFound.getImgs()!=null && lostAndFound.getImgs().length()>0){
				criteria.andImgsLike("%"+lostAndFound.getImgs()+"%");
			}			if(lostAndFound.getLocation()!=null && lostAndFound.getLocation().length()>0){
				criteria.andLocationLike("%"+lostAndFound.getLocation()+"%");
			}			if(lostAndFound.getPublishtime()!=null && lostAndFound.getPublishtime().length()>0){
				criteria.andPublishtimeLike("%"+lostAndFound.getPublishtime()+"%");
			}			if(lostAndFound.getPhone()!=null && lostAndFound.getPhone().length()>0){
				criteria.andPhoneLike("%"+lostAndFound.getPhone()+"%");
			}	
		}
		
		Page<Lostandfound> page= (Page<Lostandfound>)lostAndFoundMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
